package org.openkoala.koala.deploy.tree;

import java.util.List;

import org.eclipse.jface.viewers.CheckStateChangedEvent;
import org.eclipse.jface.viewers.ICheckStateListener;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.dialogs.ContainerCheckedTreeViewer;
import org.openkoala.koala.deploy.pojo.Itree;

/**
 * Displays a single-selection tree, a multi-selection tree, and a checkbox tree
 */
public abstract class SelectionUI<T> {

	private Button cancel;

	protected Button deSelectAll;

	private Button okButton;

	protected Button selectAll;

	protected Shell shell;

    ContainerCheckedTreeViewer treeViewer;

	public SelectionUI(Shell shell,String title) {
		this.shell = shell;
		this.drawUI(shell);
		this.initEvent();
		this.shell.setText(title);
	}

	public void drawUI(Shell shell) {

		shell.setSize(580, 480);

		shell.setLayout(new FormLayout());

		Label label = new Label(shell, SWT.NONE);
		label.setText("选择你要公布的接口及方法:");
		FormData data = new FormData();
		data.left = new FormAttachment(0, 5);
		data.top = new FormAttachment(0, 10);
		label.setLayoutData(data);

		treeViewer = new ContainerCheckedTreeViewer(shell, SWT.BORDER
				| SWT.H_SCROLL);
		treeViewer.setLabelProvider(new EJBTreeLabelProvider());
		treeViewer.setContentProvider(new EJBTreeContentProvider());
		treeViewer.setCheckStateProvider(new EJBTreeContentProvider());
		data = new FormData();
		data.left = new FormAttachment(label, 0, SWT.LEFT);
		data.top = new FormAttachment(label, 10);
		data.right = new FormAttachment(75, 0);
		data.bottom = new FormAttachment(75, 0);
		treeViewer.getTree().setLayoutData(data);

		selectAll = new Button(shell, SWT.PUSH);
		selectAll.setText("Select All");
		data = new FormData();
		data.left = new FormAttachment(treeViewer.getTree(), 10);
		data.top = new FormAttachment(treeViewer.getTree(), 0, SWT.TOP);
		data.right = new FormAttachment(selectAll, 100, SWT.LEFT);
		data.bottom = new FormAttachment(selectAll, 30, SWT.TOP);
		selectAll.setLayoutData(data);

		deSelectAll = new Button(shell, SWT.PUSH);
		deSelectAll.setText("Deselect All");
		data = new FormData();
		data.left = new FormAttachment(selectAll, 0, SWT.LEFT);
		data.top = new FormAttachment(selectAll, 15);
		data.right = new FormAttachment(deSelectAll, 100, SWT.LEFT);
		data.bottom = new FormAttachment(deSelectAll, 30, SWT.TOP);
		deSelectAll.setLayoutData(data);

		okButton = new Button(shell, SWT.PUSH);
		okButton.setText("OK");
		data = new FormData();
		data.left = new FormAttachment(100, -300);
		data.top = new FormAttachment(100, -50);
		data.right = new FormAttachment(okButton, 100, SWT.LEFT);
		data.bottom = new FormAttachment(okButton, 30, SWT.TOP);
		okButton.setLayoutData(data);

		cancel = new Button(shell, SWT.PUSH);
		cancel.setText("cancel");
		data = new FormData();
		data.left = new FormAttachment(okButton, 30);
		data.top = new FormAttachment(okButton, 0, SWT.TOP);
		data.right = new FormAttachment(cancel, 100, SWT.LEFT);
		data.bottom = new FormAttachment(cancel, 30, SWT.TOP);
		cancel.setLayoutData(data);
	}

	public void open() {
		this.shell.open();
	}
	
	public void close(){
		this.shell.close();
	}

	/**
	 * 初始化数据
	 * 
	 * @param ejbDeploy
	 */
	public abstract void initData(T t);

	public abstract void process();

	


	private void initEvent() {
		this.selectAll.addSelectionListener(new SelectionListener() {

			@Override
			public void widgetSelected(SelectionEvent e) {
				java.util.List<Itree> trees = (java.util.List) treeViewer
						.getInput();
				for (Itree tree : trees) {
					setSelected(tree, true);
					treeViewer.refresh();
				}
			}

			@Override
			public void widgetDefaultSelected(SelectionEvent e) {

			}
		});

		this.deSelectAll.addSelectionListener(new SelectionListener() {

			@Override
			public void widgetSelected(SelectionEvent e) {
				java.util.List<Itree> trees = (java.util.List) treeViewer
						.getInput();
				for (Itree tree : trees) {
					setSelected(tree, false);
					treeViewer.refresh();
				}
			}

			@Override
			public void widgetDefaultSelected(SelectionEvent e) {

			}
		});

		this.cancel.addSelectionListener(new SelectionListener() {

			@Override
			public void widgetSelected(SelectionEvent e) {
				shell.close();
			}

			@Override
			public void widgetDefaultSelected(SelectionEvent e) {
			}
		});

		this.okButton.addSelectionListener(new SelectionListener() {

			@Override
			public void widgetSelected(SelectionEvent e) {
				process();
			}

			@Override
			public void widgetDefaultSelected(SelectionEvent e) {
			}
		});
		
		treeViewer.addCheckStateListener(new ICheckStateListener(){
			
			@Override
			public void checkStateChanged(CheckStateChangedEvent event) {
				boolean val = event.getChecked();
				Itree tree = (Itree)event.getElement();
				tree.setCheck(val);
				
				List<Itree> children = tree.getChildren();
				if (children != null && !children.isEmpty()) {
					for (Itree child : children) {
						child.setCheck(val);
					}
				}
			}});
	}

	private void setSelected(Itree tree, boolean check) {
		tree.setCheck(check);
		java.util.List<Itree> children = tree.getChildren();
		if (children != null && children.size() > 0) {
			for (Itree child : children) {
				setSelected(child, check);
			}
		}
	}
}
